/**
 * 行政区划分
 *
 * @param map   地图
 * @param layIdType   模块名称 dwcl 或者 tq
 * @param methodType  点击回调方法名称
 * @param data        数据
 * @param deleteValue  是新增还是删除 字符串‘true’为删除，其他字符串为新增
 * @param deleteList    存放创建的el 方便删除使用
 */
function regionDivide(map, layIdType, methodType, data,deleteValue,deleteList) {

    // 删除marker
    if (deleteValue == "true"){
        for (var i = 0; i <deleteList.length ; i++) {
            deleteList[i].remove()
        }
    }

    // 根据模块名称获取layId
    var obj = typeFilter(layIdType, "regionDivide");
    var layId = obj.layId;
    var sourceId = obj.sourceId;

    var districtTask = new epgis.DistrictTask();

    // 行政区划名称查询行政区划
    districtTask.searchDistrict({
        keyword: "320000",
        subdistrict: 1,
        extension: true,
        pageIndex: "",
        pageSize: ""
    }).then(function (result) {
        console.log(result);

        var features = [];
        for (var o in result[0].sub_districts) {// o 再循环中是0,1,2,3这种
            var color = "#d2f7ff";// 天气模块进来默认颜色
            if (layIdType === "dwcl") {// 如果是电网潮流进来
                // 此处应该有一个方法  输入地势和符合俩个参数 来判断 返回他的颜色
                color = getColorByFh(result[0].sub_districts[o].name, data);
            }

            //给各个市添加数据
            features.push({
                'type': 'Feature',
                'geometry': result[0].sub_districts[o].shape,
                'properties': {
                    fillcolor: color,
                    data: result[0].sub_districts[o].name
                }
            });
            if (layIdType === "tq") {
                // 此处应该有方法 输入地势的天气返回不同的gif图地址
                var weatherGifUrl = getGifByData(result[0].sub_districts[o].name, data);

                if (deleteValue != "true"){
                    //画marker点
                    var el = document.createElement('div');
                    el.className = layIdType;
                    el.style.backgroundImage = 'url(' + weatherGifUrl + ')';
                    el.style.backgroundRepeat = 'no-repeat';
                    el.style.width = '50px';
                    el.style.height = '50px';
                    el.innerHTML = "<div style='margin-top:50px;margin-left:2px;color:white;'><span style='font-size:14px'>" + result[0].sub_districts[o].name +
                        "</span></div>";
                    new epgis.Marker(el, {
                        anchor: 'bottom'
                    })
                        .setLngLat(result[0].sub_districts[o].center)
                        .addTo(map);
                    // 添加进数组用于删除
                    deleteList.push(el)
                }



            }
            if (layIdType === "dwcl") {
                // 根据电网潮流的不同的实时负荷显示不同的值
                var ssfhValue = 0;
                for (var i = 0; i < data.length; i++) {
                    if (result[0].sub_districts[o].name == data[i].name) {
                        ssfhValue = data[i].ssfh
                    }
                }

                if (deleteValue != "true") {
                    //画marker点
                    var el = document.createElement('div');
                    el.className = layIdType;
                    el.style.backgroundImage = 'url("./icon/xzhf/mapbg.png")';
                    el.style.backgroundRepeat = 'no-repeat';
                    el.style.width = '90px';
                    el.style.height = '38px';
                    el.innerHTML = "<div style='margin-left:18px;margin-top:3px;color:#037f7f;font-size:14px'><span>" + ssfhValue + "MW" + "</span></div><div style='margin-top:14px;margin-left:25px'><span >" + result[0].sub_districts[o].name +
                        "</span></div>";


                    new epgis.Marker(el, {
                        anchor: 'bottom'
                    })
                        .setLngLat(result[0].sub_districts[o].center)
                        .addTo(map);
                    // 添加进数组用于删除
                    deleteList.push(el)
                }

            }

        }
        // layid和sourceid
        if (deleteValue == "true"){
            map.getLayer(layId) && map.removeLayer(layId);
            map.getSource(sourceId) && map.removeSource(sourceId);
        }else {

            //重设面数据
            map.addSource(sourceId, {
                type: 'geojson',
                data: {
                    'type': 'FeatureCollection',
                    'features': features
                }
            });

            var color = "#fff";
            if (layIdType === "tq") {
                color= "#2e94f2"
            }
            //画面
            map.addLayer({
                "id": layId,
                'type': 'fill',
                'source': sourceId,
                'paint': {
                    'fill-color': ["get", "fillcolor"],
                    'fill-opacity': .7,
                    "fill-outline-color": color
                }
            });
        }


    });

    // map.addControl(new epgis.ScaleControl({
    //     maxWidth: 80,
    //     unit: 'metric'
    // }), 'bottom-left');

    if (deleteValue != "true"){
        map.on("click", layId, function (e) {
            console.log(e.features[0].properties);
            window.parent.postMessage({
                data: {type: methodType, data: e.features[0].properties.data}
            }, '*');
        });
    }

}

/** 判断城市的天气gif
 *
 * @param cityName 城市名称
 * @param data    数据
 */
function getGifByData(cityName, data) {

    var weather = "晴";
    for (var i = 0; i < data.length; i++) {
        if (data[i].name == cityName) {
            weather = data[i].tq;
        }
    }

    if (weather == "冰雹") {
        return "./icon/tianqi/bingbao.gif";
    }
    if (weather == "大风") {
        return "./icon/tianqi/dafeng.gif";
    }
    if (weather == "大雪") {
        return "./icon/tianqi/daxue.gif";
    }
    if (weather == "大雨") {
        return "./icon/tianqi/map-dayu.png";
    }
    if (weather == "多云") {
        return "./icon/tianqi/map-duoyun.png";
    }
    if (weather == "雷") {
        return "./icon/tianqi/lei.gif";
    }
    if (weather == "雷阵雨") {
        return "./icon/tianqi/map-leizhenyu.png";
    }
    if (weather == "晴") {
        return "./icon/tianqi/mapqing.png";
    }
    if (weather == "晴转阴") {
        return "./icon/tianqi/qingzhuanyin.gif";
    }
    if (weather == "小雪") {
        return "./icon/tianqi/xiaoxue.gif";
    }
    if (weather == "小雨") {
        return "./icon/tianqi/map-xiaoyu.png";
    }
}


/** 根据实时负荷判断城市的颜色
 *
 * @param cityName 城市名称
 * @param data    数据
 */
function getColorByFh(cityName, data) {
    var ssfh = 0;
    for (var i = 0; i < data.length; i++) {
        if (data[i].name == cityName) {
            ssfh = data[i].ssfh;
        }
    }

    if (ssfh > 10000) {
        return "#ce5757";
    } else if (ssfh > 5000) {
        return "#dac409";
    } else if (ssfh > 3000) {
        return "#72bf7b";
    } else if (ssfh <= 3000) {
        return "#c9c9c9";
    }
}